<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller {
	//管理员列表
	public function admin_list(){
		$admin=M('admin');
		$count = $admin -> count(); // 查询满足要求的总记录数
		$Page = new\ Think\ Page($count, 10); 
		// 实例化分页类 传入总记录数和每页显示的条数

		$Page->setConfig('theme', '<li><a>共 %TOTAL_ROW% 条记录</a> %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% ');
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出            		
		$list = $admin -> order('id ASC') -> limit($Page -> firstRow.','.$Page -> listRows) -> select();
		$this -> assign('adminlist', $list); // 赋值数据集	
		//查询auth表
		$authlist=M('auth')->select();
		$authdata=$this->assign('authlist',$authlist);	
		$this -> display();		
		
		
 
	}
	//管理员添加
	public function admin_add(){
		$admin=D('admin');
		if(IS_POST){
			$Admindata['username']=I('username');
			$Admindata['password']=MD5(I('pwd'));
			$Admindata['mobile']=I('mobile');
			$Admindata['roleid']=I('roleid');
			$Admindata['email']=I('email');
			$Admindata['realname']=I('realname');
			$Admindata['lastloginip']=get_client_ip();
			$Admindata['lastlogintime']=time();
			$Admindata['lock']=I('lock');
			$Admindata['content']=I('content');

			if($admin->data($Admindata)->add()){
				$this->success('管理员添加成功!',U('admin_list'));
			}else{
				$this->error('管理员添加失败!');
			}		
		}else{
		$authlist=M('auth')->select();
		$this->assign('authlist',$authlist);
		$this->display();			
		}
	}
	//修改管理员
	public function admin_edit(){
		$admin=M('admin');
		$admindata['id']=I('id');

		if(IS_POST){
			if(I('pwd')){
			$admindata['password']=MD5(I('pwd'));
			}
			$admindata['mobile']=I('mobile');
			$admindata['roleid']=I('roleid');
			$admindata['email']=I('email');
			$admindata['realname']=I('realname');
			$admindata['lastloginip']=get_client_ip();
			$admindata['lastlogintime']=time();
			$admindata['lock']=I('lock');
			$admindata['content']=I('content');
       		$save=$admin->save($admindata);
			if($save !== false){
				$this->success('管理员信息更新成功~！',U('Admin/admin_list'));
			}else{
				$this->error('管理员更新失败~！');
			}

		}else{
			$authlist=M('auth')->select();
			$this->assign('authlist',$authlist);			
			$adminfind=$admin->find($admindata['id']);
			$this->assign('adminlist',$adminfind);
			$this->display();
		}
	}

	//删除管理员
	public function admin_del(){
        $admin=D('admin');
        $id=I('id');
        if($id==1){
            $this->error('初始化管理员不能删除！');
        }else{
          if($admin->delete($id)){
            $this->success('删除管理员成功！',U('admin_list'));
        }else{
            $this->error('删除管理员失败！');
        }  
        }
	}
	//批量删除
	public function admin_del_all(){

        $ids = I('id');
        $where['id']=array("in",$ids);

        if(empty($ids)){
             $this->error("非法操作！");
        }else{
        	$admin_delall = M('admin')->where($where)->delete();
	        if ($admin_delall !== false) {
	             $this->success('删除成功',U('admin_list'));
	        }else{
	             $this->error('删除失败');
	        }	
        }	
	}
	//停用管理员
	public function admin_lock(){
        $admin=M('admin');
        $admindata['id']=I('id');
        $admindata['lock']=I('lock');
        $admindata['card']=I('card');
        if($admindata['card'] !== '1'){

	        if($admindata['lock'] == 1){
	        	$message='停用';
	        }else{
	        	$message='启用';
	        }
	        $save=$admin->save($admindata);
			if($admindata['id'] ==1){
	            $this->error('初始化管理员不能停用！');
			}else{
	        if($save !== false){
	           		$this->success('管理员'.$message.'成功！',U('Admin/admin_list'));
	        	}else{
	           		$this->error('管理员'.$message.'失败！');
	        	}  			
			}
		}else{
			$this->error('初始化管理员不能停用！');
		}
	}

}
?>